﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XGameLibrary.Sprite
{
    public class AnimatedSprite
    {
        #region Field Region

        Dictionary<Animation.Direction, Animation> animations;
        Animation.Direction direction;

        Texture2D[,] texture;
        Vector2 position;
        Vector2 velocity;
        float speed = 200.0f;

        #endregion

        #region Property Region

        public Animation.Direction Direction
        {
            get { return this.direction; }
            set
            {
                if (this.direction != value)
                {
                    this.animations[value].AniState = this.animations[direction].AniState;
                    this.direction = value;
                };
            }
        }

        public Animation.State State
        {
            get { return this.animations[direction].AniState; }
            set { this.animations[direction].AniState = value; }
        }

        public Vector2 Position
        {
            //小写position表示的是电脑画图时所用的坐标，以左上点为基准点。
            //大写Position表示的是鼠标指向时所用的坐标，以中下点为基准点。
            get { return this.position + new Vector2(Width / 2, Height); }
            set { this.position = value - new Vector2(Width / 2, Height); }
        }

        public int Width
        {
            get { return animations[Direction].FrameWidth; }
        }

        public int Height
        {
            get { return animations[Direction].FrameHeight; }
        }

        public float Speed
        {
            get { return speed; }
            set { speed = MathHelper.Clamp(speed, 1.0f, 400.0f); }
        }

        public Vector2 Velocity
        {
            get { return velocity; }
            set
            {
                velocity = value;
                if (velocity != Vector2.Zero)
                    velocity.Normalize();
            }
        }

        #endregion

        #region Constructor Region

        public AnimatedSprite(Texture2D[,] sprite, Dictionary<Animation.Direction, Animation> animation)
        {
            texture = sprite;
            animations = new Dictionary<Animation.Direction, Animation>();

            foreach (Animation.Direction direction in animation.Keys)
                animations.Add(direction, (Animation)animation[direction].Clone());
        }

        #endregion

        #region Method Region

        public void Update(GameTime gameTime)
        {
            animations[direction].Update(gameTime);
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch, float offsetX, float offsetY, float zoom)
        {
            int width = texture[(int)direction, animations[direction].CurrentFrame].Width;
            int height = texture[(int)direction, animations[direction].CurrentFrame].Height;
            spriteBatch.Draw(
                texture[(int)direction, animations[direction].CurrentFrame], new Rectangle((int)((position.X - offsetX) * zoom), (int)((position.Y - offsetY) * zoom), (int)(width * zoom), (int)(height * zoom)), Color.White);
        }

        public void LockToMap(float cameraX, float cameraY)
        {
            position.X = MathHelper.Clamp(position.X, 0, World.MapWidth - Width);
            position.Y = MathHelper.Clamp(position.Y, 0, World.MapWidth - Height);
        }

        #endregion

        public Vector2 LockToMap(Vector2 clickvec)
        {
            Vector2 vec = new Vector2();
            vec.X = MathHelper.Clamp(clickvec.X, Width / 2, World.MapWidth - Width / 2);
            vec.Y = MathHelper.Clamp(clickvec.Y, Height, World.MapWidth);
            return vec;
        }
    }
}
